﻿package mortal.game.view.npc
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import extend.language.*;
    import fl.data.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.common.net.*;
    import mortal.component.gLinkText.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.guide.*;
    import mortal.game.view.common.guide.view.*;
    import mortal.game.view.copy.crossMainCity.*;
    import mortal.game.view.copy.groupCopy.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.npc.data.*;
    import mortal.game.view.pack.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.drama.*;
    import mortal.mvc.core.*;

    public class NpcController extends Controller
    {
        private var _taskDialog:NpcTaskDialogModule;
        private var _taskAutoDialog:NpcTaskAutoDialogModule;
        private var _lastTaskList:Array;
        private var _outToCloseWindows:Array;
        public var lastOpTime:int;
        private var _chooseCache:NpcTaskChooseCache;
        private var _dicAutoGot:Dictionary;
        private var _curNpc:NPCPlayer;
        private var _curWindow:Window;

        public function NpcController()
        {
            this._dicAutoGot = new Dictionary();
            return;
        }// end function

        public function set curWindow(param1:Window) : void
        {
            this._curWindow = param1;
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingDel, this.taskDelHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskCanGetRecv, this.taskCanGetAddHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingListRecv, this.taskDoingListRecvHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskCanGetAdd, this.taskCanGetAddHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingAdd, this.taskDoingAddHandler);
            Dispatcher.addEventListener(EventName.NPC_ClickedHandler, this.clickNpcHandler);
            Dispatcher.addEventListener(EventName.NpcDialogShowTaskInfo, this.showTaskInfoHandler);
            Dispatcher.addEventListener(EventName.TaskAskNextStep, this.taskNextStepHandler);
            Dispatcher.addEventListener(EventName.NPC_ClickNpcFunction, this.npcFunctionHandler);
            FrEventDispatcher.instance.proxyAddEventListener(RolePlayer.instance, PlayerEvent.GIRD_WALK_END, this.checkDistanceHandler);
            NetDispatcher.addCmdListener(ServerCommand.MapPointUpdate, this.checkDistanceHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.checkDistanceHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskNpcShowHideUpdate, this.taskShowHideHandler);
            Dispatcher.addEventListener(EventName.NpcChooseAnswer, this.chooseAnswerHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            Dispatcher.addEventListener(EventName.HideTaskDialog, this.hideDialogReqHandler);
            Dispatcher.addEventListener(EventName.ResumeTaskDialog, this.resumeDialogReqHandler);
            Dispatcher.addEventListener(EventName.NpcClickOtherMapNpc, this.clickOtherMapNpcHandler);
            return;
        }// end function

        private function clickOtherMapNpcHandler(event:DataEvent) : void
        {
            var _loc_2:* = new GLinkTextData();
            _loc_2.type = GLinkTextData.npc;
            _loc_2.value1 = int(event.data);
            var _loc_3:* = SceneConfig.instance.getSceneInfo(_loc_2.value1);
            _loc_2.mapId = _loc_3.sMapDefine.mapId;
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTraceTargetEvent, _loc_2));
            return;
        }// end function

        public function isTaskDialogHide() : Boolean
        {
            if (this._taskDialog == null)
            {
                return true;
            }
            return this._taskDialog.isHide;
        }// end function

        private function hideDialogReqHandler(event:DataEvent) : void
        {
            if (this._taskDialog == null || this._taskDialog.isHide)
            {
                return;
            }
            this._taskDialog.hide();
            return;
        }// end function

        private function resumeDialogReqHandler(event:DataEvent) : void
        {
            if (this._taskDialog == null)
            {
                return;
            }
            this._taskDialog.resume();
            return;
        }// end function

        private function taskDoingAddHandler(param1:Object) : void
        {
            var _loc_2:* = int(param1);
            var _loc_3:* = cache.task.getTaskByCode(_loc_2);
            if (_loc_3.isComplete())
            {
                if (_loc_3.stask.autoEnd)
                {
                    this.showAutoTaskDialog(_loc_3.stask, _loc_3.stask.autoEndTalk);
                }
            }
            if (_loc_3.stask.group == ETaskGroup._ETaskGroupEscort)
            {
                return;
            }
            if (_loc_3.stask.group == ETaskGroup._ETaskGroupGuild && _loc_3.isDoing())
            {
                return;
            }
            this.showNextTaskInfo();
            return;
        }// end function

        private function taskDoingListRecvHandler(param1:Object) : void
        {
            var _loc_3:SPlayerTask = null;
            var _loc_4:TaskInfo = null;
            var _loc_2:* = param1 as Array;
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = cache.task.getTaskByCode(_loc_3.task.code);
                if (_loc_4.isComplete())
                {
                    if (_loc_4.stask.autoEnd)
                    {
                        this.showAutoTaskDialog(_loc_4.stask, _loc_4.stask.autoEndTalk);
                    }
                }
            }
            this.showNextTaskInfo(param1);
            return;
        }// end function

        private function taskCanGetAddHandler(param1:Object) : void
        {
            var _loc_3:STask = null;
            var _loc_4:Array = null;
            var _loc_5:TaskInfo = null;
            var _loc_2:* = param1 as Array;
            if (_loc_2 != null && _loc_2.length > 0)
            {
                for each (_loc_3 in _loc_2)
                {
                    
                    if (_loc_3.autoGet && this._dicAutoGot[_loc_3.code] == null)
                    {
                        this.showAutoTaskDialog(_loc_3, _loc_3.autoGetTalk);
                        this._dicAutoGot[_loc_3.code] = true;
                        continue;
                    }
                    if (cache.task.getGroupFinishCount(ETaskGroup._ETaskGroupLoop) > 0 && _loc_3.group == ETaskGroup._ETaskGroupLoop)
                    {
                        _loc_4 = [];
                        _loc_5 = cache.task.getTaskByCode(_loc_3.code);
                        if (_loc_5 != null)
                        {
                            _loc_4.push(_loc_5);
                            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, _loc_4));
                        }
                    }
                }
            }
            this.showNextTaskInfo();
            return;
        }// end function

        public function hideAllModules() : void
        {
            if (this._taskAutoDialog != null && !this._taskAutoDialog.isHide)
            {
                this._taskAutoDialog.hide();
            }
            if (this._taskDialog != null && !this._taskDialog.isHide)
            {
                this._taskDialog.hide();
            }
            return;
        }// end function

        private function showAutoTaskDialog(param1:STask, param2:int) : void
        {
            var _loc_4:String = null;
            var _loc_5:TTaskDialog = null;
            var _loc_6:Array = null;
            var _loc_7:NpcTaskAutoDialogData = null;
            var _loc_3:* = cache.task.getTaskByCode(param1.code);
            if (param2 == 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [_loc_3, null]));
            }
            else
            {
                if (this._taskAutoDialog == null)
                {
                    this._taskAutoDialog = new NpcTaskAutoDialogModule();
                }
                this._taskAutoDialog.show();
                _loc_4 = "";
                _loc_5 = TaskConfig.instance.getDialog(param2);
                if (_loc_5 != null)
                {
                    _loc_4 = _loc_5.talkStr;
                }
                else
                {
                    Log.error("找策划，ttaskdialog.json没配置项， 任务对话id = " + param2);
                }
                _loc_6 = NpcTaskAutoDialogData.parse(_loc_4, _loc_3);
                this._taskAutoDialog.showTaskTalk(_loc_6);
                _loc_7 = _loc_6[0] as NpcTaskAutoDialogData;
                if (_loc_7 != null)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [_loc_3, _loc_7.npcInfo]));
                }
            }
            if (this._taskDialog != null && !this._taskDialog.isHide)
            {
                this._taskDialog.hide();
            }
            return;
        }// end function

        private function chooseAnswerHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as NpcChooseData;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = _loc_2.info.stask.endChoose - 1;
            if (_loc_2.info.isCanget())
            {
                _loc_3 = _loc_2.info.stask.getChoose - 1;
            }
            var _loc_4:* = cache.npc.selectedNpc;
            if (_loc_4 == null || _loc_4.npcInfo == null)
            {
                return;
            }
            if (_loc_2.index != _loc_3)
            {
                this._chooseCache.setChosen(_loc_2.info, _loc_2.index);
                this.showTaskInfo(_loc_2.info, _loc_4.npcInfo);
            }
            else
            {
                this.npcDialog.showTaskInfo(_loc_2.info, _loc_4.npcInfo, Language.getString(21048));
            }
            return;
        }// end function

        private function sceneUpdateHandler(event:DataEvent) : void
        {
            this.taskShowHideHandler(null);
            return;
        }// end function

        private function taskShowHideHandler(param1:Object) : void
        {
            var _loc_2:String = null;
            ThingUtil.npcUtil.updateNpcToDefaultShow(MapFileUtil.mapID);
            var _loc_3:* = cache.npc.getShowNpc();
            for (_loc_2 in _loc_3)
            {
                
                ThingUtil.npcUtil.updateNpcIsAddToStage(parseInt(_loc_2), _loc_3[_loc_2] as Boolean, MapFileUtil.mapID);
            }
            ThingUtil.passUtil.resetNotAddToStage();
            _loc_3 = cache.npc.getShowCopy();
            for (_loc_2 in _loc_3)
            {
                
                ThingUtil.passUtil.updatePassIsAddToStage(parseInt(_loc_2), _loc_3[_loc_2] as Boolean);
            }
            return;
        }// end function

        private function npcFunctionHandler(event:DataEvent) : void
        {
            var data:NpcFunctionData;
            var nextFramClickFunc:Function;
            var evt:* = event;
            nextFramClickFunc = function () : void
            {
                dispatchNpcFuncEvent(data);
                return;
            }// end function
            ;
            data = evt.data as NpcFunctionData;
            if (this._taskDialog != null && !this._taskDialog.isHide)
            {
                this._curWindow = null;
                Dispatcher.removeEventListener(EventName.WindowClosed, this.npcWinCloseHandler);
                this._taskDialog.hide();
            }
            CallLater.addCallBack(nextFramClickFunc);
            return;
        }// end function

        private function dispatchNpcFuncEvent(param1:NpcFunctionData) : void
        {
            this._outToCloseWindows = [];
            Dispatcher.addEventListener(EventName.WindowShowed, this.npcFunctionWindowShowHandler);
            CallLater.addCallBack(this.nextFrameClickNpcFunc);
            switch(param1.type)
            {
                case ENPCFunType._ENPCFunShop:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.NPC_OpenNpcShop, param1));
                    break;
                }
                case ENPCFunType._ENPCFunCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.CopyClickCopyLink, param1));
                    break;
                }
                case ENPCFunType._ENPCFunWarehouse:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.WarehouseOpen, param1));
                    break;
                }
                case ENPCFunType._ENPCFunArena:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaOpenOrCloseMainWin));
                    break;
                }
                case ENPCFunType._ENPCFunBattlefield:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BattlefieldEnterWindowShow));
                    break;
                }
                case ENPCFunType._ENPCFunSingleCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyOpenByIndex, GroupCopyModule.SingleCopy));
                    break;
                }
                case ENPCFunType._ENPCFunGroupCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyOpenByIndex, GroupCopyModule.GroupCopy));
                    break;
                }
                case ENPCFunType._ENPCFunChallengeCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ChallengeCopyOpen));
                    break;
                }
                case ENPCFunType._ENPCFunDefenseCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyOpenByIndex, GroupCopyModule.DefenCopy));
                    break;
                }
                case ENPCFunType._ENPCFunEnterGuildCopy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_enterManor, 0));
                    break;
                }
                case ENPCFunType._ENPCFunGuildWar:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarEnterWindowShow, true));
                    break;
                }
                case ENPCFunType._ENPCFunGuildBoss:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildActivityShow));
                    break;
                }
                case ENPCFunType._ENPCFunOpenForging:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.OpenForgingPanel, ForgingConst.Page_EquipStrengthen));
                    break;
                }
                case ENPCFunType._ENPCFunOpenPet:
                {
                    GameManager.instance.popupModuleType(ModuleType.Pets);
                    break;
                }
                case ENPCFunType._ENPCFunPetThrough:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopyOpenByIndex));
                    break;
                }
                case ENPCFunType._ENPCFunOpenMount:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BestShowMountWin));
                    break;
                }
                case ENPCFunType._ENPCFunOpenWizard:
                {
                    GameManager.instance.popupModuleType(ModuleType.Wizard);
                    break;
                }
                case ENPCFunType._ENPCFunCycleTask:
                {
                    break;
                }
                case ENPCFunType._ENPCFunHangTask:
                {
                    break;
                }
                case ENPCFunType._ENPCFunPKTask:
                {
                    break;
                }
                case ENPCFunType._ENPCFunSieveTask:
                {
                    break;
                }
                case ENPCFunType._ENPCFunCrossTask:
                {
                    break;
                }
                case ENPCFunType._ENPCFunOpenMarket:
                {
                    GameManager.instance.popupModuleType(ModuleType.Market);
                    break;
                }
                case ENPCFunType._ENPCFunMiningTask:
                {
                    GameController.mining.showModule(true);
                    break;
                }
                case ENPCFunType._ENPCFunEscortTask:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.EscortCopyOpenSelectTaskWindow));
                    break;
                }
                case ENPCFunType._ENPCFunChangeCareer:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ChangeCareerShowModule, true));
                    break;
                }
                case ENPCFunType._ENPCFuncHeroArena:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HeroArenaOpenMainWin));
                    break;
                }
                case ENPCFunType._ENPCFuncHeroArenaRestRoom:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HeroArenaOpenRestRoom));
                    break;
                }
                case ENPCFunType._ENPCFuncCityTax:
                {
                    if (cache.mainCity.canIGetTaxReward())
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.MainCityModuleOpenTabIndex, MainCityModule.Index_FinalPanel));
                    }
                    else if (cache.mainCity.sMainCityPositons != null && cache.guild.isMyGuild(cache.mainCity.sMainCityPositons.guild))
                    {
                        MsgManager.showRollTipsMsg(Language.getString(21529));
                    }
                    else
                    {
                        MsgManager.showRollTipsMsg(Language.getString(21530));
                    }
                    break;
                }
                case ENPCFunType._ENPCFuncBlackMarket:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BlckMarketOpen));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function npcFunctionWindowShowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Window;
            if (!(_loc_2 is PackModule))
            {
                Dispatcher.removeEventListener(EventName.WindowShowed, this.npcFunctionWindowShowHandler);
                this._curWindow = _loc_2;
            }
            this._outToCloseWindows.push(_loc_2);
            return;
        }// end function

        private function nextFrameClickNpcFunc() : void
        {
            Dispatcher.removeEventListener(EventName.WindowShowed, this.npcFunctionWindowShowHandler);
            if (this._curWindow == null && this._curNpc != null)
            {
                this._curNpc.updateIsTalkToRole(false);
            }
            else
            {
                Dispatcher.addEventListener(EventName.WindowClosed, this.npcErJiWinCloseHandler);
            }
            return;
        }// end function

        private function npcErJiWinCloseHandler(event:DataEvent) : void
        {
            var _loc_2:Window = null;
            var _loc_3:NPCPlayer = null;
            if (this._curNpc != null && this._curNpc.npcInfo != null && this._curWindow != null)
            {
                _loc_2 = event.data as Window;
                if (_loc_2 == this._curWindow)
                {
                    Dispatcher.removeEventListener(EventName.WindowClosed, this.npcErJiWinCloseHandler);
                    this._curNpc.updateIsTalkToRole(false);
                    _loc_3 = ThingUtil.npcUtil.getNpc(this._curNpc.npcInfo.snpc.npcId);
                    if (_loc_3)
                    {
                        _loc_3.closeSpecialWindow();
                    }
                    if (!(this._curWindow is NpcTaskDialogModule) && !(this._curWindow is NpcTaskAutoDialogModule))
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.NpcFuncWinClosed, this._curNpc.npcInfo));
                    }
                    this._curWindow = null;
                }
            }
            return;
        }// end function

        private function nextFrameClickNpc() : void
        {
            Dispatcher.removeEventListener(EventName.WindowShowed, this.clickNpcWidowShowHandler);
            return;
        }// end function

        private function clickNpcWidowShowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Window;
            this._curNpc = ThingUtil.selectEntity as NPCPlayer;
            if (this._curNpc != null && _loc_2 != null)
            {
                if (!(_loc_2 is PackModule))
                {
                    this._curWindow = _loc_2;
                }
                if (!GameDefConfig.instance.isDoNotTargetToPlayer(this._curNpc.npcInfo.tnpc.code))
                {
                    this._curNpc.updateIsTalkToRole(true);
                }
                Dispatcher.addEventListener(EventName.WindowClosed, this.npcWinCloseHandler);
            }
            return;
        }// end function

        private function npcWinCloseHandler(event:DataEvent) : void
        {
            var _loc_2:Window = null;
            var _loc_3:NPCPlayer = null;
            if (this._curNpc != null && this._curWindow != null)
            {
                _loc_2 = event.data as Window;
                if (_loc_2 == this._curWindow)
                {
                    Dispatcher.removeEventListener(EventName.WindowClosed, this.npcWinCloseHandler);
                    this._curNpc.updateIsTalkToRole(false);
                    if (this._curNpc.npcInfo != null && this._curNpc.npcInfo.snpc != null)
                    {
                        _loc_3 = ThingUtil.npcUtil.getNpc(this._curNpc.npcInfo.snpc.npcId);
                        if (_loc_3)
                        {
                            _loc_3.closeSpecialWindow();
                        }
                    }
                    if (!(this._curWindow is NpcTaskDialogModule) && !(this._curWindow is NpcTaskAutoDialogModule))
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.NpcFuncWinClosed, this._curNpc.npcInfo));
                    }
                    this._curWindow = null;
                }
            }
            return;
        }// end function

        private function clickNpcHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as NPCPlayer;
            if (_loc_2 == null)
            {
                return;
            }
            if (cache.task.isDrama || TaskDramaOperator.isInFlyDrama)
            {
                return;
            }
            CallLater.addCallBack(this.nextFrameClickNpc);
            Dispatcher.addEventListener(EventName.WindowShowed, this.clickNpcWidowShowHandler);
            cache.npc.selectedNpc = _loc_2;
            cache.npc.selectedNpcMapId = Game.mapInfo.mapId;
            this.showNpcFuncPanel(_loc_2.npcInfo);
            return;
        }// end function

        public function showNpcFuncPanel(param1:NPCInfo) : void
        {
            this._lastTaskList = cache.npc.getTaskListByNpcId(param1.tnpc.code);
            if (this.checkAndShowMainTask(this._lastTaskList, param1))
            {
                return;
            }
            if (TaskRule.shouldShowTaskFunc(this._lastTaskList))
            {
                this.showNpcFunctionList(param1);
            }
            else
            {
                this.showTaskList(this._lastTaskList, param1);
            }
            return;
        }// end function

        private function checkAndShowMainTask(param1:Array, param2:NPCInfo) : Boolean
        {
            var _loc_4:TaskInfo = null;
            var _loc_3:int = 0;
            while (_loc_3 < param1.length)
            {
                
                _loc_4 = param1[_loc_3];
                if (_loc_4.stask.group != ETaskGroup._ETaskGroupMain)
                {
                }
                else if (_loc_4.isCanget() || _loc_4.isComplete() && !_loc_4.isCanNotEnd())
                {
                    this.showTaskList([_loc_4], param2);
                    return true;
                }
                _loc_3++;
            }
            return false;
        }// end function

        private function taskNextStepHandler(event:DataEvent) : void
        {
            if (this._lastTaskList == null || this._lastTaskList.length == 0)
            {
                this.hideDialog();
                return;
            }
            var _loc_2:* = event.data as Array;
            if (_loc_2 == null)
            {
                this.hideDialog();
                return;
            }
            var _loc_3:* = _loc_2[0] as TaskInfo;
            var _loc_4:* = _loc_2[1] as NPCInfo;
            if (_loc_4 == null)
            {
                return;
            }
            var _loc_5:* = _loc_4.tnpc.code;
            var _loc_6:* = _loc_3.stask.code;
            if (TaskUtil.isDramaStep(_loc_3, (_loc_3.curStep + 1)))
            {
                this.hideDialog();
                return;
            }
            this.lastOpTime = getTimer();
            return;
        }// end function

        private function hideDialog() : void
        {
            if (this._taskDialog == null || this._taskDialog.isHide)
            {
                return;
            }
            this._taskDialog.hide();
            return;
        }// end function

        private function taskUpdateHandler(param1:Object) : void
        {
            var _loc_3:SPlayerTaskUpdate = null;
            var _loc_4:TaskInfo = null;
            var _loc_5:Array = null;
            var _loc_6:Array = null;
            var _loc_7:GLinkTextData = null;
            var _loc_8:String = null;
            var _loc_2:* = param1 as Array;
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = cache.task.getTaskByCode(_loc_3.taskCode);
                if (_loc_4.isComplete())
                {
                    if (_loc_4.stask.autoEnd)
                    {
                        this.showAutoTaskDialog(_loc_4.stask, _loc_4.stask.autoEndTalk);
                    }
                    continue;
                }
                _loc_5 = cache.task.getLinkTextData(_loc_4.stask.code);
                if (_loc_5 == null)
                {
                    continue;
                }
                _loc_6 = _loc_5[_loc_4.curStep];
                if (_loc_6 == null || _loc_6.length != 1)
                {
                    continue;
                }
                _loc_7 = _loc_6[0];
                if (_loc_7 == null || _loc_7.type != GLinkTextData.self)
                {
                    continue;
                }
                _loc_8 = cache.npc.getTaskTalk(_loc_7.data as TaskInfo);
                if (_loc_8 == null || _loc_8 == "")
                {
                }
                this.npcDialog.show();
                this.npcDialog.showSelf(_loc_4, _loc_8);
                return;
            }
            this.showNextTaskInfo();
            return;
        }// end function

        private function taskDelHandler(param1:Object = null) : void
        {
            var _loc_2:* = param1 as TaskInfo;
            this.showNextTaskInfo(param1);
            return;
        }// end function

        public function showNextTaskInfo(param1:Object = null) : void
        {
            var _loc_3:TaskInfo = null;
            var _loc_5:TaskInfo = null;
            if (AutoGuideController.isGuiding)
            {
                return;
            }
            if (AutoGuideNewFunc.isShowing || AutoGuideNewSkill.isShowing)
            {
                AutoGuideNewFunc.hasCloseTaskWin = true;
                return;
            }
            var _loc_2:* = cache.npc.selectedNpc;
            if (_loc_2 == null || _loc_2.npcInfo == null)
            {
                return;
            }
            if (this._taskDialog && !this._taskDialog.isHide && this._taskDialog.showMode == NpcTaskDialogModule.ShowMode_TaskInfo)
            {
                return;
            }
            this._lastTaskList = cache.npc.getTaskListByNpcId(_loc_2.npcInfo.tnpc.code);
            if (this.checkAndShowMainTask(this._lastTaskList, _loc_2.npcInfo))
            {
                return;
            }
            if (this._lastTaskList == null || this._lastTaskList.length == 0)
            {
                return;
            }
            var _loc_4:int = 0;
            while (_loc_4 < this._lastTaskList.length)
            {
                
                _loc_5 = this._lastTaskList[_loc_4] as TaskInfo;
                if (TaskRule.isNotAutoShowNextTask(_loc_5))
                {
                }
                else
                {
                    _loc_3 = _loc_5;
                    break;
                }
                _loc_4++;
            }
            if (_loc_3 != null)
            {
                if (TaskRule.shouldShowTaskFunc([_loc_3]))
                {
                    this.showNpcFunctionList(_loc_2.npcInfo);
                }
                else
                {
                    this.showTaskInfo(_loc_3, _loc_2.npcInfo);
                }
            }
            return;
        }// end function

        private function showTaskInfoHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as TaskInfo;
            var _loc_3:* = cache.npc.selectedNpc;
            if (_loc_3 == null || _loc_3.isDisposed)
            {
                return;
            }
            this.showTaskList([_loc_2], _loc_3.npcInfo);
            return;
        }// end function

        private function showTaskList(param1:Array, param2:NPCInfo) : void
        {
            var _loc_4:DataProvider = null;
            var _loc_5:TaskInfo = null;
            var _loc_6:TaskInfo = null;
            var _loc_7:int = 0;
            var _loc_3:* = param1[0];
            if (param1.length == 1)
            {
                this.showTaskInfo(_loc_3, param2);
            }
            else
            {
                _loc_4 = new DataProvider();
                _loc_7 = 0;
                while (_loc_7 < param1.length)
                {
                    
                    _loc_3 = param1[_loc_7];
                    if (_loc_3.isCanget())
                    {
                        _loc_5 = _loc_3;
                    }
                    if (_loc_3.isComplete() && !_loc_3.isCanNotEnd())
                    {
                        this.showTaskInfo(_loc_3, param2);
                        return;
                    }
                    if (_loc_3.isCanNotEnd())
                    {
                        _loc_6 = _loc_3;
                    }
                    _loc_4.addItem(param1[_loc_7]);
                    _loc_7++;
                }
                if (_loc_5 != null && _loc_6 != null)
                {
                    this.showTaskInfo(_loc_5, param2);
                    return;
                }
                this.npcDialog.show();
                this.npcDialog.showTaskList(_loc_4, param2, param2.tnpc.talk);
            }
            return;
        }// end function

        private function showTaskInfo(param1:TaskInfo, param2:NPCInfo) : void
        {
            if (cache.guild.isInGuild)
            {
                if (cache.guild.isInSelfGuild && param1 != null)
                {
                    if (TaskUtil.isGuildTask(param1.stask))
                    {
                        if (this._taskDialog != null && !this._taskDialog.isHide)
                        {
                            this._taskDialog.hide();
                        }
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TaskGuildShowGetWin, param1));
                        return;
                    }
                }
                else if (param2.tnpc.talk != null && param2.tnpc.talk != "")
                {
                    this.npcDialog.show();
                    this.npcDialog.showNpcDefaultTalk(param2, param2.tnpc.talk);
                    return;
                }
            }
            var _loc_3:* = cache.npc.getTaskTalk(param1);
            if (this._chooseCache == null)
            {
                this._chooseCache = new NpcTaskChooseCache();
            }
            var _loc_4:* = this._chooseCache.parseForChoose(param1, _loc_3);
            this.npcDialog.show();
            if (_loc_4 == null || _loc_4.length == 0)
            {
                this.npcDialog.showTaskInfo(param1, param2, _loc_3);
            }
            else
            {
                this.npcDialog.showChoose(_loc_4, param1, param2, _loc_3.substr(0, _loc_3.lastIndexOf("{")));
            }
            return;
        }// end function

        private function showNpcFunctionList(param1:NPCInfo) : void
        {
            var _loc_7:NpcFunctionData = null;
            var _loc_2:* = param1.tnpc.effect;
            if (_loc_2 == null || _loc_2 == "")
            {
                if (cache.npc.isBySceneClick)
                {
                    this.npcDialog.show();
                    this.npcDialog.showNpcDefaultTalk(param1, param1.tnpc.talk);
                }
                return;
            }
            _loc_2 = _loc_2.replace(/\[""\[/g, "");
            var _loc_3:* = _loc_2.split("]");
            var _loc_4:* = new DataProvider();
            var _loc_5:* = cache.role.roleInfo.level;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3.length)
            {
                
                _loc_2 = _loc_3[_loc_6];
                if (_loc_2 == null || _loc_2 == "")
                {
                }
                else
                {
                    _loc_7 = new NpcFunctionData();
                    _loc_7.parse(_loc_2);
                    if ((_loc_7.minLv != 0 || _loc_7.maxLv != 0) && (_loc_5 < _loc_7.minLv || _loc_5 > _loc_7.maxLv))
                    {
                    }
                    else
                    {
                        _loc_7.npcId = param1.snpc.npcId;
                        _loc_4.addItem(_loc_7);
                    }
                }
                _loc_6++;
            }
            if (_loc_4.length == 1)
            {
                this.dispatchNpcFuncEvent(_loc_7);
            }
            else
            {
                this.npcDialog.show();
                this.npcDialog.showNpcFunction(_loc_4, param1, param1.tnpc.talk);
            }
            return;
        }// end function

        public function get npcDialog() : NpcTaskDialogModule
        {
            if (this._taskDialog == null)
            {
                this._taskDialog = new NpcTaskDialogModule();
            }
            return this._taskDialog;
        }// end function

        private function checkDistanceHandler(param1 = null) : void
        {
            if (cache.npc.selectedNpc == null)
            {
                return;
            }
            Global.instance.callLater(this.nextFrameClearNpcWin);
            return;
        }// end function

        private function nextFrameClearNpcWin() : void
        {
            var _loc_1:Window = null;
            if (!this.isInDistance())
            {
                if (this._taskDialog != null && !this._taskDialog.isHide)
                {
                    this._taskDialog.hide();
                }
                if (ThingUtil.selectEntity == cache.npc.selectedNpc)
                {
                    ThingUtil.selectEntity = null;
                }
                if (this._outToCloseWindows != null && this._outToCloseWindows.length > 0)
                {
                    for each (_loc_1 in this._outToCloseWindows)
                    {
                        
                        if (_loc_1 != null && !_loc_1.isHide)
                        {
                            _loc_1.hide();
                        }
                    }
                    this._outToCloseWindows = null;
                }
                cache.npc.selectedNpc = null;
                Dispatcher.dispatchEvent(new DataEvent(EventName.NPC_OutCurNpcDistance));
            }
            return;
        }// end function

        public function isInDistance() : Boolean
        {
            if (cache.npc.selectedNpc == null)
            {
                return false;
            }
            if (Game.mapInfo != null && cache.npc.selectedNpcMapId != Game.mapInfo.mapId)
            {
                return false;
            }
            var _loc_1:* = GeomUtil.calcDistance(RolePlayer.instance.realX2d, RolePlayer.instance.realY2d, cache.npc.selectedNpc.x2d, cache.npc.selectedNpc.y2d);
            if (_loc_1 >= GameConst.NpcOutDistance)
            {
                return false;
            }
            return true;
        }// end function

    }
}
